package com.eqdd.yiqidian.share;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;

import com.eqdd.yiqidian.ListItem89Custom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.GridSpacingItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.bean.rebuild.GridBean;

import java.util.ArrayList;

import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.qqmodel.QZone;
import cn.jiguang.share.wechat.Wechat;
import cn.jiguang.share.wechat.WechatFavorite;
import cn.jiguang.share.wechat.WechatMoments;


/**
 * @author吕志豪 .
 * @date 18-4-14  下午3:39.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class JShareDialog {

    private ImgShareBuilder imgShareBuilder;
    private TextShareBuilder textShareBuilder;
    private LinkShareBuilder linkShareBuilder;
    private Context baseActivity;
    private BottomSheetDialog shareDialog;

    private boolean isTextShare = false;
    private boolean isImgShare = false;
    private boolean isLinkShare = false;

    public JShareDialog(ImgShareBuilder imgShareBuilder, Context baseActivity) {
        this.baseActivity = baseActivity;
        this.imgShareBuilder = imgShareBuilder;
        isImgShare = true;
        initDialog();
    }

    public JShareDialog(TextShareBuilder textShareBuilder, Context baseActivity) {
        this.baseActivity = baseActivity;
        this.textShareBuilder = textShareBuilder;
        isTextShare = true;
        initDialog();
    }

    public JShareDialog(LinkShareBuilder linkShareBuilder, Context baseActivity) {
        this.baseActivity = baseActivity;
        this.linkShareBuilder = linkShareBuilder;
        isLinkShare = true;
        initDialog();
    }


    private void initDialog() {
        shareDialog = new BottomSheetDialog(baseActivity);
        ListItem89Custom inflate = DataBindingUtil.inflate(LayoutInflater.from(baseActivity), R.layout.list_item_89, null, false);
        inflate.recyclerView.setLayoutManager(new GridLayoutManager(baseActivity, 4));
        inflate.recyclerView.setNestedScrollingEnabled(false);
        inflate.recyclerView.addItemDecoration(new GridSpacingItemDecoration(4, 50, true));
        ArrayList<GridBean> gridBeans = new ArrayList<>();
        gridBeans.add(new GridBean("发送给好友", R.mipmap.share_friend, 0));
        if (isLinkShare)
            gridBeans.add(new GridBean("工作圈", R.mipmap.share_eqd, 1));
        gridBeans.add(new GridBean("我的收藏", R.mipmap.share_shoucang, 2));
        if (isLinkShare)
            gridBeans.add(new GridBean("用浏览器打开", R.mipmap.share_liulanqi, 3));
        gridBeans.add(new GridBean("微信", R.mipmap.wechat, 4));
        gridBeans.add(new GridBean("微信收藏", R.mipmap.wechat_fav, 5));
        gridBeans.add(new GridBean("朋友圈", R.mipmap.wechat_moment, 6));
        if (!isTextShare)
            gridBeans.add(new GridBean("QQ", R.mipmap.qq, 7));
        gridBeans.add(new GridBean("qq空间", R.mipmap.qzone, 8));
        gridBeans.add(new GridBean("新浪微博", R.mipmap.weibo, 9));
        SlimAdapterEx<GridBean> slimAdapterEx = SlimAdapterEx.create().register(R.layout.grid_item, new SlimInjector<GridBean>() {
            @Override
            public void onInject(GridBean data, IViewInjector injector) {
                injector.text(R.id.ItemText, data.getTitle())
                        .image(R.id.ItemImage, data.getResId())
                        .gone(R.id.red_dot);
            }
        }).attachTo(inflate.recyclerView).updateData(gridBeans);
        ItemClickSupport.addTo(inflate.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    JShareUtil share = shareTo();
                    switch (slimAdapterEx.getDataItem(position).getPos()) {

                        case 0:
                            if (share != null)
                                share.share2EQD();
                            break;
                        case 1:
                            if (share != null)
                                share.share2WorkCircle();
                            break;
                        case 2:
                            if (share != null)
                                share.share2Collection();
                            break;
                        case 3:
                            if (share != null)
                                share.share2Explorer();
                            break;
                        case 4:
                            if (share != null)
                                share.share2WeXin(Wechat.Name);
                            break;
                        case 5:
                            if (share != null)
                                share.share2WeXin(WechatFavorite.Name);
                            break;
                        case 6:
                            if (share != null)
                                share.share2WeXin(WechatMoments.Name);
                            break;
                        case 7:
                            if (share != null)
                                share.share2Q(QQ.Name);
                            break;
                        case 8:
                            if (share != null)
                                share.share2Q(QZone.Name);
                            break;
                        case 9:
                            if (share != null)
                                share.share2Sina();
                            break;
                    }


                    shareDialog.dismiss();
                });
        shareDialog.setContentView(inflate.getRoot());
        shareDialog.setCancelable(true);
        shareDialog.setCanceledOnTouchOutside(true);
    }

    private JShareUtil shareTo() {
        if (isImgShare)
            return new JShareUtil(imgShareBuilder, baseActivity);
        if (isTextShare)
            return new JShareUtil(textShareBuilder, baseActivity);
        if (isLinkShare)
            return new JShareUtil(linkShareBuilder, baseActivity);
        return null;
    }

    public void show() {
        shareDialog.show();
    }
}
